<?php
namespace app\actions;

use yii\base\Action;
use yii\base\Model;
class FormAction extends Action {
	public $model;
	
	public function runWithParams($params) {
		try {
			//@var Model
			$model = is_array($this->model) ? \Yii::createObject($this->model) : $this->model;
			$model->setAttributes(\Yii::$app->request->post());
			$action = str_replace(' ', '', ucwords(str_replace('-', ' ', $this->id)));
			$data = $model->$action();
			return ['data' => $data, 'status' => 0];
		} catch (\app\exceptions\FormParamNotValidateException $e) {
			return ['errors' => $e->model->getErrors(), 'status' => $e->getCode()];
		} catch (\app\exceptions\BusinessException $e) {
			return ['error' => $e->getMessage(), 'status' => $e->getCode()];
		}
	}
}